$(function () {
    //功能1 获取用户信息，渲染页面
    getUserInfo()
    
    //功能2 登出页面
    $('.logout').on('click',function(index){
        layer.confirm('是否确定退出登录？',{icon:3,tittle:'提示'},function(){

            location.href = '/login.html'
            localStorage.removeItem('token')

            layer.close(index);
        })
    })



})


function getUserInfo() {
    axios({
        url:'/my/userinfo',
        method:'get',
        //在拦截器设置 headers
    }).then(({data:res})=>{
        //成功回调
        // console.log(res)
        if(res.status != 0){
            return layui.layer.msg(res.message)
        }
        //渲染页面
        renderAvatar(res.data)
    });
}

//封装渲染函数
function renderAvatar(user){

    let name = user.nickname || user.username
    $('.welcome').html('欢迎 '+name)

    if(user.user_pic !=null){
        $('.avatar-test').hide()
        $('.layui-nav-img').show().attr('src',user.user_pic)
    }else{
        $('.layui-nav-img').hide()
        let first = name[0].toUpperCase()
        $('.avatar-test').show().html(first)
    }



}